import {
	showLoading,
	hideLoading
} from './loading'
const request = ({
	url = '',
	data = {},
	method = 'POST',
	header = {},
	responseType = 'text',
	mask = true
}) => {
	const token = uni.getStorageSync("token") || ''
	const noMask = data && data.noMask || !mask
	return new Promise((resolve, reject) => {
		if (!noMask) showLoading()
		uni.request({
			method: method.toUpperCase(),
			url,
			data: data,
			header: {
				"token": token,
				...header
			},
			dataType: 'json',
			responseType,
			success: (response) => {
				if ((response.data && response.data.code === "1000") || response.statusCode ==
					"1000") {
					uni.setStorageSync('token', null);
					// 当URL是这些的时候   就不进行跳转
					uni.reLaunch({
						url: '/pages/login/index'
					});
					uni.showToast({
						title: JSON.stringify(response.data.msg) || '登录失效，请重新登录',
						icon: "none",
						duration: 2000
					});

					resolve(response.data);
					return false;
				}
				if (response.data && response.data.code !== 200) {
					uni.showToast({
						title: response.data.msg || '请求失败',
						icon: "none",
						duration: 2000
					});
					resolve(response.data);
					return false;
				}
				resolve(response.data);
			},
			fail: (error) => {
				if (error.errMsg == 'request:fail') {
					uni.showToast({
						title: "网络连接异常请联系管理员",
						icon: "none",
						duration: 2000
					});
				}
				reject(error)
			},
			complete() {
				if (!noMask) hideLoading()
			},
		})
	});
}
export default request